package com.ykzb.base.dao.impl;

import java.io.Serializable;

import com.ykzb.base.dao.BaseDao;
import com.ykzb.base.mapper.BaseMapper;
import com.ykzb.util.IdGenerator;
import com.ykzb.util.TopUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;




public abstract class BaseDaoImpl<T, E extends Serializable> implements BaseDao<T, E>
{
	private BaseMapper<T, E> baseMapper;

	protected Log log = LogFactory.getLog(this.getClass().getName());

	/**
	 * 字类必须重写此方法,把实体对应的Mapper设置进去
	 * 
	 * @param baseMapper
	 */
	public void setBaseMapper(BaseMapper<T, E> baseMapper)
	{
		this.baseMapper = baseMapper;
	}

	@Override
	public int insert(T object)
	{
		// 如果此对象没有设置ID值则设置一个UUID值
		try
		{
			if (!TopUtil.isValidValue(BeanUtils.getProperty(object, "id")))
			{
				BeanUtils.setProperty(object, "id", IdGenerator.getCode(19, 0));
			}
		} catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		return baseMapper.insert(object);
	}

	@Override
	public int update(T object)
	{
		return baseMapper.update(object);
	}

	@Override
	public int delete(E id)
	{
		return baseMapper.delete(id);
	}

	@Override
	public T select(E id)
	{
		return baseMapper.select(id);
	}
}
